package com.wframe.project.former.dataservices.products;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.springframework.transaction.annotation.Transactional;
import com.frame.database.JBaseService;
import com.frame.database.JServiceFactory;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.frame.database.util.Seq;
import com.wframe.project.former.dataservices.products.dao.ProductClassDao;

/**
 * 
 * 功能:商品分类数据服务<br>
 * 参数:<br>
 * 作者:王海锋<br>
 * 时间:2017-1-13上午10:58:28<br>
 */
public class ProductClassService extends JBaseService<ProductClassDao>{

	public static ProductClassService getIntance(){
        return JServiceFactory.createJServiceProxy(ProductClassService.class);
    }
	
	public List<JdataBean> loadClassList(String code) throws Exception{
		if(StringUtils.isBlank(code))return getDao().loadClassList();
		return getDao().loadClassList(code);
	}
	
	public List<JdataBean> loadClassTree(int pid) throws Exception{
		return getDao().loadClassTree(pid);
	}
	
	public JdataBean getClassByCode(String code) throws Exception{
		return getDao().getClassByCode(code);
	}
	
	@Transactional
	public void deleteClass(String id) throws Exception{
		JdataBean bean=getBean(id);
		del(id);
		getDao().getSession().delete("update wb_products set status=1 where id in (select productid from wb_products_and_class where (classcode=? or classcode like ?))",new Object[]{bean.getString("code"),bean.getString("code")+".%"});//下架商品
		getDao().getSession().delete("delete from wb_products_and_class where (classcode=? or classcode like ?)",new Object[]{bean.getString("code"),bean.getString("code")+".%"});
	}
	/**
	 * 
	 * 功能:获得模型属性列表<br>
	 * 参数:tag:1基本属性、商品扩展属性、3销售属性<br>
	 * 作者:王海锋<br>
	 * 时间:2017-1-9下午11:11:30<br>
	 */
	public List<JdataBean> queryModelAttr(String classcode,Integer tag) throws Exception{
		String sql="select * from wb_products_attr where classcode=? and status<>-1 ";
		if(tag!=null)sql+=" and tag="+tag;
		sql+=" order by orderno,id";
		return getDao().getSession().query(sql, new Object[]{classcode});
	}
	
	
	public List<JdataBean> queryModelAttrItem(String attrid) throws Exception{
		return getDao().queryModelAttrItem(attrid);
	}
	public String saveModelAttr(JdataBean bean) throws Exception{
		if(StringUtils.isNotBlank(bean.getString("id"))){//修改记录
			Map key=new HashMap();
			key.put("id", bean.getString("id"));
			getDao().getSession().update("wb_products_attr", bean.getDataMap(),key);
			return bean.getString("id");
		}else{//新增记录
			bean.put("status", 1);
			String id=getDao().getSession().saveReturnKey("wb_products_attr", bean.getDataMap());
			return id;
		}
	}
	
	public JdataBean getModelAttr(String id) throws Exception{
		return getDao().getModelAttr(id);
	}
	
	public void delModelAttr(String id) throws Exception{
		getDao().delModelAttr(id);
	}
	public String saveModelAttrItem(JdataBean bean) throws Exception{
		if(StringUtils.isNotBlank(bean.getString("id"))){//修改记录
			Map key=new HashMap();
			key.put("id", bean.getString("id"));
			getDao().getSession().update("wb_products_attritem", bean.getDataMap(),key);
			return bean.getString("id");
		}else{//新增记录
			String id=getDao().getSession().saveReturnKey("wb_products_attritem", bean.getDataMap());
			getDao().getSession().update("update wb_products_attritem set code=? where id=?",new Object[]{id,id});
			return id;
		}
	}
	public JdataBean getModelAttrItem(String id) throws Exception{
		return getDao().getSession().getObject("select * from wb_products_attritem where id=?", new Object[]{id});
	}
	
	public void delModelAttrItem(String id) throws Exception{
		getDao().getSession().update("update wb_products_attritem set status=-1 where id=?",new Object[]{id});
	}
	/**
	 * 
//	 * 功能:根据值编码查询模型属性值<br>
	 * 参数:<br>
	 * 作者:王海锋<br>
	 * 时间:2017-1-17上午10:05:03<br>
	 * @throws Exception 
	 */
	public List<JdataBean> queryModelAttrItemByCodes(List<String> attrcodes) throws Exception{
		if(attrcodes!=null && attrcodes.size()>0){
			return getDao().queryModelAttrItemByCodes(attrcodes);
		}else return new ArrayList<JdataBean>();
	}
	/**
	 * 
	 * 功能:查询商品分类归属层级<br>
	 * 参数:<br>
	 * 作者:王海锋<br>
	 * 时间:2017-1-14下午11:42:43<br>
	 */
	public List<JdataBean> getProductClassNav(String classcode) throws Exception{
		return getDao().getProductClassNav(classcode);
	}
	
	public String getProductClassName(String productid) throws Exception{
		return getDao().getProductClassName(productid);
	}
	
	public PageResult queryFeatured(String code,int sise,int pageno) throws Exception{
		return getDao().queryFeatured(code, sise, pageno);
	}
	
	public void featuredSave(String code,String[] pids) throws Exception{
		if(pids!=null){
			for(String ids:pids){
				String productid=ids.split("_")[0];
				String priceid=ids.split("_")[1];
				JdataBean get=getDao().getFeatured(code, priceid);
				if(get==null)
					getDao().saveFeatured(code, productid, priceid);
			}
		}
	}
	public void featuredDel(String id) throws HibernateException, Exception{
		getDao().getSession().delete("delete from wb_class_featured where id=?",new Object[]{id});
	}
	
	public List<JdataBean> getFeaturedProducts(String code) throws Exception{
		return getDao().getFeaturedProducts(code);
	}
	
	
	/**
	 * 
	 * 功能:创建商品分类编码<br>
	 * 参数:<br>
	 * 作者:王海锋<br>
	 * 时间:2017-1-14下午11:49:48<br>
	 */
	public String createClassCode(String parcode) throws Exception{
		String nextid=new Seq().getKeyId("wb_products_class_code").toString();
		return parcode+"."+nextid;
	}
}
